package com.lianzhihui.romeex.widget.dialog;

import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.EcologicalBean;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.MyToast;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.NumberUtils;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me.dialog
 * @ClassName: UpDateVersionDialog
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/3 10:40
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/3 10:40
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ChoseIntegralDialog extends BaseDialog implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.radio_layout)
    RadioGroup radioLayout;
    String count = "";
    String type = "";
    @BindView(R.id.tv_title)
    TextView title;
    @BindView(R.id.erc20)
    RadioButton erc20;
    @BindView(R.id.trc20)
    RadioButton trc20;
    @BindView(R.id.ret)
    RadioButton ret;
    Callback callback;
    EcologicalBean bean;

    public ChoseIntegralDialog(@NonNull Context context, EcologicalBean bean) {
        super(context);
        this.bean = bean;
        type = "";
        count = "";
    }

    public ChoseIntegralDialog(Context context, int style) {
        super(context, style);
    }

    public void setDialogListener(Callback callback) {
        this.callback = callback;
    }

    @Override
    public void setCancelable(boolean flag) {
        super.setCancelable(flag);
    }


    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_integral_chose;
    }

    @Override
    protected void initView() {
        radioLayout.setOnCheckedChangeListener(this);
        radioLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

            }
        });
//        if (Double.parseDouble(bean.getErc20UsdtBalance()) < Double.parseDouble(bean.getUsdtTotalPrice())) {
////            erc20.setEnabled(false);
////            erc20.setTextColor(MyApplication.mContext.getResources().getColor(R.color.text_cccccc_color));
//        }
//        if (Double.parseDouble(bean.getTrc20UsdtBalance()) < Double.parseDouble(bean.getUsdtTotalPrice())) {
////            trc20.setEnabled(false);
////            trc20.setTextColor(MyApplication.mContext.getResources().getColor(R.color.text_cccccc_color));
//        }
//        double v = Double.parseDouble(bean.getUsdtTotalPrice()) / bean.getRetCoinPrice();
//        if (Double.parseDouble(bean.getRetBalance()) < v) {
////            ret.setEnabled(false);
////            ret.setTextColor(MyApplication.mContext.getResources().getColor(R.color.text_cccccc_color));
//        }
        erc20.setText(MessageFormat.format("{0}USDT(ERC20)", bean.getUsdtTotalPrice()));
        trc20.setText(MessageFormat.format("{0}USDT(TRC20)", bean.getUsdtTotalPrice()));
        ret.setText(MessageFormat.format("{0}RET", NumberUtils.big8(Double.parseDouble(bean.getUsdtTotalPrice()) / bean.getRetCoinPrice())));
    }


    @OnClick({R.id.iv_close, R.id.tv_confirm})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.iv_close:
                    dismiss();
                    break;
                case R.id.tv_confirm:
                    if (null != callback) {
                        if (type.isEmpty()) {
                            //未操作
//                            dismiss();
                            MyToast.showToast(MyApplication.mContext.getResources().getString(R.string.text_buying_patterns));
                        } else {
                            //已经选择
                            callback.choseResult(type, count);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        type = "";
        count = "";
        switch (checkedId) {
            case R.id.erc20:
                if (Double.parseDouble(bean.getErc20UsdtBalance()) < Double.parseDouble(bean.getUsdtTotalPrice())) {
                    group.clearCheck();
                    MyToast.showToast(MyApplication.mContext.getResources().getString(R.string.text_node_payment_enough_details));

                    return;
                }
                type = "1";
                count = bean.getUsdtTotalPrice();
                break;
            case R.id.trc20:
                if (Double.parseDouble(bean.getTrc20UsdtBalance()) < Double.parseDouble(bean.getUsdtTotalPrice())) {
                    group.clearCheck();
                    MyToast.showToast(MyApplication.mContext.getResources().getString(R.string.text_node_payment_enough_details));

                    return;
                }
                type = "2";
                count = bean.getUsdtTotalPrice();
                break;
            case R.id.ret:
                double v = Double.parseDouble(bean.getUsdtTotalPrice()) / bean.getRetCoinPrice();
                if (Double.parseDouble(bean.getRetBalance()) < v) {
                    group.clearCheck();
                    MyToast.showToast(MyApplication.mContext.getResources().getString(R.string.text_node_payment_enough_details));

                    return;
                }
                type = "3";
                count = String.valueOf(v);
                break;
            default:
                break;
        }
    }

    public interface Callback {
        void choseResult(String type, String count);
    }
}
